<template>
<div>
  <div style="margin-bottom: 20px">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }" style="margin-left: 20px">用户管理</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">用户</a></el-breadcrumb-item>
    </el-breadcrumb>
  </div>

  <div style="margin: 10px 0">
    <!--            <el-input style="width: 200px" placeholder="请输入邮箱..." suffix-icon="el-icon-message"></el-input>-->
    <el-input style="width: 200px" placeholder="请输入姓名..." suffix-icon="el-icon-search" class="ml-5"
              v-model="xingming"></el-input>
    <!--            <el-input style="width: 200px" placeholder="请输入地址..." suffix-icon="el-icon-location"-->
    <!--                      class="ml-5"></el-input>-->
    <el-button class="ml-5" type="primary" @click="seachKehu">搜索</el-button>
    <el-button @click="resetForm">重置</el-button>
  </div>
  <div style="margin: 10px 0">
    <el-button type="primary" @click="openKehudialog">新增<i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
    <el-button type="danger" @click="delbath">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
    <el-upload
        action="http://localhost:8888/api/kehu/import" style="display: inline-block" :show-file-list="false"
        :accept="xls" :on-success="impOK" :on-error="imperr"
    >
    <el-button type="primary" class="ml-5">导入<i class="el-icon-bottom" style="margin-left: 2px"></i></el-button>
    </el-upload>
    <el-button type="primary" @click="exp" class="ml-5">导出<i class="el-icon-top" style="margin-left: 2px"></i></el-button>
  </div>

  <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
            @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55">
    </el-table-column>
    <el-table-column type="index" label="序号" width="50" fixed></el-table-column>
    <el-table-column prop="xingming" label="姓名" width="100px">
    </el-table-column>
    <el-table-column prop="xingbie" label="性别" width="70px">
    </el-table-column>
    <el-table-column prop="nianling" label="年龄" width="70px">
    </el-table-column>
    <el-table-column prop="shouji" label="手机" width="130px">
    </el-table-column>
    <el-table-column prop="youxiang" label="邮箱" width="160px">
    </el-table-column>
    <el-table-column prop="jiatingzhuzhi" label="家庭地址" width="160px">
    </el-table-column>
    <el-table-column prop="jiaofeiqingkuang" label="缴费情况" width="160px">
    </el-table-column>
    <el-table-column prop="fangwuxinxi" label="房屋信息" width="160px">
    </el-table-column>
    <el-table-column prop="fangchanziliao" label="房产资料" width="160px">
    </el-table-column>
    <el-table-column prop="zhaopian" label="照片" width="160px">
      <template slot-scope="scope">
        <img :src="scope.row.zhaopian" min-width="80" height="80"/>
      </template>
    </el-table-column>
    <el-table-column label="操作"  fixed="right" width="200px">
      <template slot-scope="scope">
        <el-button type="primary"  @click="updatekehu(scope.row)"><i class="el-icon-edit"></i>编辑</el-button>
        <el-popconfirm
            class="ml-5"
            confirm-button-text='确定'
            cancel-button-text='取消'
            icon="el-icon-info"
            icon-color="red"
            title="确定要删除这条记录吗？"
            @confirm="deletekehu(scope.row.id)"
        >
          <el-button type="danger" slot="reference"><i class="el-icon-remove-outline"></i>删除</el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="padding: 10px 0">
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10,15,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>

  <el-dialog title="客户信息" :visible.sync="dialogFormVisible" width="60%">
    <el-form :inline="true"  :model="form" label-position="left">
      <el-form-item label="姓名" label-width="100px">
          <el-input v-model="form.xingming" autocomplete="off" placeholder="请输入姓名" style="margin-left: -50px"></el-input>
      </el-form-item>
      <el-form-item label="性别" label-width="100px">
          <el-select v-model="form.xingbie" placeholder="性别..." style="margin-left: -50px;width: 150px">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="年龄" label-width="100px" style="margin-left: 50px">
          <el-input type="number" v-model="form.nianling" autocomplete="off" placeholder="请输入年龄" style="margin-left: -50px"></el-input>
      </el-form-item>
      <el-form-item label="手机" label-width="100px">
          <el-input v-model="form.shouji" autocomplete="off" placeholder="请输入手机" style="margin-left: -50px"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" label-width="100px">
          <el-input v-model="form.youxiang" autocomplete="off" placeholder="请输入邮箱" style="margin-left: -50px;width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="缴费情况" label-width="100px" style="margin-left: 50px">
        <el-input v-model="form.jiaofeiqingkuang" autocomplete="off" style="margin-left: -40px;width: 143px"></el-input>
      </el-form-item>
      <el-form-item label="家庭地址" label-width="100px" >
          <el-input v-model="form.jiatingzhuzhi" autocomplete="off" type="textarea" style="margin-left: -40px"></el-input>
      </el-form-item>
      <el-form-item label="房屋信息" label-width="100px">

          <el-input v-model="form.fangwuxinxi" autocomplete="off" type="textarea" style="margin-left: -40px"></el-input>

      </el-form-item>
      <el-form-item label="房产资料" label-width="100px">

          <el-input v-model="form.fangchanziliao" autocomplete="off" type="textarea" style="margin-left: -40px"></el-input>

      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="saveKehu">确 定</el-button>
    </div>
  </el-dialog>



</div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "User",
  data(){
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      xingming: "",
      form:{
        id:"",
        xingming:"",
        xingbie:"",
        nianling:"",
        shouji:"",
        youxiang:"",
        jiatingzhuzhi:"",
        jiaofeiqingkuang:"",
        fangwuxinxi:"",
        fangchanziliao:""
      },
      dialogFormVisible: false,
      imageUrl: '',
      headerBg: 'headerBg',
      multipleSelection: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    imperr(){
      this.$message.error("文件导入失败")
    },
    impOK(){
      this.$message.success("文件导入成功")
      this.load()
    },
    exp(){
window.open("http://localhost:8888/api/kehu/export");
    },
    delbath(){
      let ids=this.multipleSelection.map(v =>v.id)
      request.post("kehu/deletebatch",ids).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除客户成功")
        } else {
          this.$message.error("删除客户失败")
        }
      })
    },
    deletekehu(id) {
      console.log(id)
      request.post("kehu/delete",id).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除客户成功")
        } else {
          this.$message.error("删除客户失败")
        }
      })
    },
    updatekehu(row){
      this.form=row
      this.dialogFormVisible=true
    },
    saveKehu(){
      this.dialogFormVisible = false
      if (this.form.id){
        request.post("kehu/update",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("修改客户信息成功")
          }else {
            this.$message.error("修改客户信息失败")
          }
        })

      }else {
        request.post("kehu",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("新增客户成功")
          }
        })
      }
    },
    openKehudialog(){
      this.form={}
      this.dialogFormVisible=true
    },
    resetForm() {
      this.xingming = ""
      this.load()
    },
    seachKehu() {
      this.load()
    },
    handleCurrentChange(data) {
      this.pageNum = data
      this.load()
    },
    handleSizeChange(data) {
      this.pageSize = data
      this.load()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    load() {
      request.get("kehu/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          xingming: this.xingming
        }
      }).then(res => {
        if (res.code=="200"){
          this.tableData = res.data.records
          this.total = res.data.total
        }

      })
    }
  }

}
</script>

<style scoped>

</style>